package main

import (
	"encoding/json"

	"jlpay-sdk-go/pkg/jlpay/demo"
	"jlpay-sdk-go/pkg/jlpay/models"
	"jlpay-sdk-go/pkg/jlpay/services"
	"jlpay-sdk-go/pkg/jlpay/utils"
)

// 预下单示例
func preOrderDemo(cashierService *services.CashierService) {
	// 创建预下单请求
	request := models.NewPreOrderRequest()

	// 直接使用map构建请求
	requestMap := map[string]interface{}{
		"merch_no":           "849584358120018",
		"term_no":            "60131234",
		"out_trade_no":       "ld201911023465529",
		"payment_valid_time": "10",
		"description":        "充值中心-会员充值",
		"attach":             "商城—-易拉宝10个打印纸20箱",
		"total_amount":       "1",
		"remark":             "该订单物流请走顺丰快递",
		"notify_url":         "https://help.jlpay.com/api/docs/home",
		"env_version":        "release",
		"merch_appid":        "wx336dcaf6a1ecf632",
		"return_url":         "page/home/webview/webview?src=https%3A%2F%2Fmp.elong.com%2Ftenthousandaura%2F%3Factivitycode%3Dd32d7735-ae4f-440c-bb0c-dba4deba9470%26isSocket%3DHotelActivityId_8407&isRefresh=refresh&isSocket=HotelActivityId_8407**ecf632",
	}

	// 设置原始请求数据
	request.SetRawRequestMap(requestMap)

	// 序列化请求数据并打印
	requestJSON, _ := json.Marshal(requestMap)
	utils.Debugf("预下单请求数据: %s", string(requestJSON))

	// 发送请求
	response, err := cashierService.CreatePreOrder(request)
	if err != nil {
		utils.Errorf("预下单请求失败: %v", err)
		return
	}

	// 处理响应 - 使用BaseResponse接口的HandleResponse方法
	response.HandleResponse("预下单")
}

func main() {
	// 设置日志级别为info，显示详细的调试信息
	utils.SetLogLevel("info")

	utils.Info("=============================")
	utils.Info("===== 预下单示例 开始 =====")
	utils.Info("=============================")

	// 创建配置对象和收银台服务
	config := demo.CreateDemoConfig()
	cashierService := services.NewCashierService(config)

	preOrderDemo(cashierService)

	utils.Info("=============================")
	utils.Info("===== 预下单示例执行完毕 =====")
	utils.Info("=============================")
}
